package bug;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.sun.xml.internal.ws.util.StringUtils;

import property.Property;
import utils.Splitter;
import utils.Stem;
import utils.Stopword;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BugCorpusCreatorMy {


	private void writeCorpus(Bug bug, FileWriter writer) throws IOException {
		String[] splitWords = Splitter.splitNatureLanguage(bug.getBugSummary());
		StringBuffer corpus = new StringBuffer();
		for (String word : splitWords) {
			word = Stem.stem(word.toLowerCase());
			//word = word.toLowerCase();
			if (!Stopword.isEnglishStopword(word)) {
				corpus.append(word + " ");
			}
		}
		corpus.append("\t");
		splitWords = Splitter.splitNatureLanguage(bug.getBugDescription());
		for (String word : splitWords) {
			word = Stem.stem(word.toLowerCase());
			//word = word.toLowerCase();
			if (!Stopword.isEnglishStopword(word)) {
				corpus.append(word + " ");
			}
		}
		writer.write(corpus.toString().trim()+ Property.getInstance().getLineSeparator());
		writer.flush();
	}
	private void writeCorpus2(Bug bug, String storeDir) throws IOException {

		String content = bug.getBugSummary();
		String[] splitWords = Splitter.splitNatureLanguage(content);
		StringBuffer corpus = new StringBuffer();
		for (String word : splitWords) {
			word = Stem.stem(word.toLowerCase());
			//word = word.toLowerCase();
			if (!Stopword.isEnglishStopword(word)) {
				corpus.append(word + " ");
			}
		}
		FileWriter writer = new FileWriter(storeDir + bug.getBugId() + "_1.txt");
		writer.write(corpus.toString().trim());
		writer.flush();
		writer.close();
		content = bug.getBugDescription();
		splitWords = Splitter.splitNatureLanguage(content);
		corpus = new StringBuffer();
		for (String word : splitWords) {
			word = Stem.stem(word.toLowerCase());
			//word = word.toLowerCase();
			if (!Stopword.isEnglishStopword(word)) {
				corpus.append(word + " ");
			}
		}
		FileWriter writer2 = new FileWriter(storeDir + bug.getBugId() + "_2.txt");
		writer2.write(corpus.toString().trim());
		writer2.flush();
		writer2.close();
	}
	private ArrayList<Bug> parseXML() {
		ArrayList<Bug> list = new ArrayList<Bug>();
		DocumentBuilderFactory domFactory = DocumentBuilderFactory
				.newInstance();
		try {
			DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
			InputStream is = new FileInputStream(Property.getInstance()
					.getBugFilePath());
			Document doc = domBuilder.parse(is);
			Element root = doc.getDocumentElement();
			NodeList bugRepository = root.getChildNodes();
			if (bugRepository != null) {
				for (int i = 0; i < bugRepository.getLength(); i++) {
					Node bugNode = bugRepository.item(i);
					if (bugNode.getNodeType() == Node.ELEMENT_NODE) {
						String bugId = bugNode.getAttributes()
								.getNamedItem("id").getNodeValue();
						String openDate = bugNode.getAttributes()
								.getNamedItem("opendate").getNodeValue();
						String fixDate = bugNode.getAttributes()
								.getNamedItem("fixdate").getNodeValue();
						Bug bug = new Bug();
						bug.setBugId(bugId);
						bug.setOpenDate(openDate);
						bug.setFixDate(fixDate);
						for (Node node = bugNode.getFirstChild(); node != null; node = node
								.getNextSibling()) {
							if (node.getNodeType() == Node.ELEMENT_NODE) {
								if (node.getNodeName().equals("buginformation")) {
									NodeList _l = node.getChildNodes();
									for (int j = 0; j < _l.getLength(); j++) {
										Node _n = _l.item(j);
										if (_n.getNodeName().equals("summary")) {
											String summary = _n
													.getTextContent();
											bug.setBugSummary(summary);
										}

										if (_n.getNodeName().equals(
												"description")) {
											String description = _n
													.getTextContent();
											bug.setBugDescription(description);
										}

									}

								}
								if (node.getNodeName().equals("fixedFiles")) {
									NodeList _l = node.getChildNodes();
									for (int j = 0; j < _l.getLength(); j++) {
										Node _n = _l.item(j);
										if (_n.getNodeName().equals("file")) {
											String fileName = _n
													.getTextContent();
											bug.addFixedFile(fileName);
										}
									}
								}
							}
						}
						list.add(bug);
					}
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return list;
	}

	
	public void create() throws IOException {

		ArrayList<Bug> list = new BugCorpusCreatorMy().parseXML();
		/*String dir2Path = Property.getInstance().getWorkDir()
				+ Property.getInstance().getSeparator() + "BugCorpusSplit"
				+ Property.getInstance().getSeparator();*/
		Property.getInstance().setBugReportCount(list.size());
		FileWriter writerContent = new FileWriter(Property.getInstance().getWorkDir()
				+ Property.getInstance().getSeparator() + "BugCorpus.txt");
		FileWriter writer = new FileWriter(Property.getInstance().getWorkDir()
				+ Property.getInstance().getSeparator() + "SortedId.txt");
		FileWriter writerFix = new FileWriter(Property.getInstance()
				.getWorkDir()
				+ Property.getInstance().getSeparator()
				+ "FixLink.txt");
		FileWriter writerClassName = new FileWriter(Property.getInstance().getWorkDir() + Property.getInstance().getSeparator() + "DescriptionClassName.txt");

		for (Bug bug : list) {
			writeCorpus(bug, writerContent);
			writer.write(bug.getBugId() + "\t" + bug.getOpenDate()+ "\t" + bug.getFixDate()
					+ Property.getInstance().getLineSeparator());

			writer.flush();
			StringBuffer setjoin =new StringBuffer();
            for (String fixName : bug.set) {
				setjoin.append(fixName+" ");
			}
            writerFix.write(setjoin + Property.getInstance().getLineSeparator());
            writerFix.flush();
			String classnames = extractClassName(bug.getBugDescription());
			writerClassName.write(bug.getBugId() + "\t" + classnames + Property.getInstance().getLineSeparator());
		}
		writerClassName.close();
		writer.close();
		writerFix.close();
		writerContent.close();
	}
	
//	public void create() throws IOException {
//
//		ArrayList<Bug> list = new BugCorpusCreatorMy().parseXML();
//		String dirPath = Property.getInstance().getWorkDir()
//				+ Property.getInstance().getSeparator() + "BugCorpus"
//				+ Property.getInstance().getSeparator();
//		/*String dir2Path = Property.getInstance().getWorkDir()
//				+ Property.getInstance().getSeparator() + "BugCorpusSplit"
//				+ Property.getInstance().getSeparator();*/
//		File file = new File(dirPath);
//		Property.getInstance().setBugReportCount(list.size());
//		if (!file.exists())
//			file.mkdir();
//		//File file2 = new File(dir2Path);
//		//if (!file2.exists())
//			//file2.mkdir();	
//		FileWriter writer = new FileWriter(Property.getInstance().getWorkDir()
//				+ Property.getInstance().getSeparator() + "SortedId.txt");
//		FileWriter writerFix = new FileWriter(Property.getInstance()
//				.getWorkDir()
//				+ Property.getInstance().getSeparator()
//				+ "FixLink.txt");
//		FileWriter writerClassName = new FileWriter(Property.getInstance().getWorkDir() + Property.getInstance().getSeparator() + "DescriptionClassName.txt");
//
//		for (Bug bug : list) {
//			writeCorpus(bug, dirPath);
//			writer.write(bug.getBugId() + "\t" + bug.getOpenDate()+ "\t" + bug.getFixDate()
//					+ Property.getInstance().getLineSeparator());
//
//			writer.flush();
//			StringBuffer setjoin =new StringBuffer();
//            for (String fixName : bug.set) {
//				setjoin.append(fixName+" ");
//			}
//            writerFix.write(setjoin + Property.getInstance().getLineSeparator());
//            writerFix.flush();
//			String classnames = extractClassName(bug.getBugDescription());
//			writerClassName.write(bug.getBugId() + "\t" + classnames + Property.getInstance().getLineSeparator());
//		}
//		writerClassName.close();
//		writer.close();
//		writerFix.close();
//	}

	public String extractClassName(String content){

		String pattern = "[a-zA-Z_][a-zA-Z0-9_\\-]*\\.java";
		StringBuffer res = new StringBuffer();

		// Create a Pattern object
		Pattern r = Pattern.compile(pattern);

		// Create matcher object.
		Matcher m = r.matcher(content);
		while (m.find()) {
			res.append(m.group(0) + " ");
		}
		return res.toString();
	}
	public static void main(String[] args) {
		StringBuffer strtest = new StringBuffer();
		strtest.append("A;");
		strtest.append("B;");
		strtest.append("C;");
	}

}
